<?php
class ActivationService
{
	/**
	 * 激活用户
	 * @Author   leikes
	 * @datetime 2016-02-15T14:12:57+0800
	 * @param    array                   $registerNumber 注册序列号关联数组
	 * @return   exception                                   [code,message]
	 */
	public function activation($registerNumber)
	{
		try
		{
			DB::transaction(function() use($registerNumber)
			{
				$this->verifyRegisterNumber($registerNumber);
				$userId=Auth::id();
				$user=User::find($userId);
				$serverInfo=Serverinfo::where('register_number','=',$registerNumber)->first();
				$serverInfo->is_use=true;
				$user->is_activation=true;
				$serverInfo->save();
				$user->save();
			});
			throw new NormalException;
		}
		catch(FieldValueAuthException $e)
		{
			return $e;
		}
		catch(RegisterNumberException $e)
		{
			return $e;
		}
		catch(NormalException $e)
		{
			return $e;
		}
	}

	private function verifyRegisterNumber($registerNumber)
	{
		$rules=[
			'register_number'        =>'required'
		];
		$validation = Validator::make($registerNumber,$rules);
		if( $validation->fails() )
		{
			throw new FieldValueAuthException( $validation->messages()->first() );
		}
		$rules=[
			'register_number'        =>'exists:serverinfos,register_number,is_use,0'
		];
		$validation = Validator::make($registerNumber,$rules);
		if( $validation->fails() )
		{
			throw new RegisterNumberException;
		}
	}
}
?>